﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KP_CommonLibrary.KP_Interface;
using KP_CommonLibrary;
using DevComponents.DotNetBar;
using KP_CommonLibrary.KP_Function;
using DevComponents.AdvTree;
using KEEPER_BRR_13dotNET.BusinessLogic;
using KP_CommonLibrary.KP_CommonControls;
using KP_CommonLibrary.Logs;

namespace KEEPER_BRR_13dotNET.Presentation
{
    public partial class Ucl_DsHoSoPhaiTra : KP_CommonLibrary.KP_CommonControls.KP_UserControl, IRunCommand
    {
        int Ma_Tt1, Ma_Tt2;
        string maVtri = "";
        BL_KPHoSo blHoSo;
        public Ucl_DsHoSoPhaiTra()
        {
            InitializeComponent();
        }
        public override bool DispatchCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.F4)
            {
                bbiTraHoSo_Click(null, null);
                return true;
            }
            if (keyData == Keys.Escape)
            {
                bbiExit_Click(null, null);
                return true;
            }
            return base.DispatchCmdKey(ref msg, keyData);
        }

        #region IRunCommand Members

        public void RunCommand(object sender, object commandData)
        {
            string _params = (commandData as DataRow)["Params"].ToString();
            Dictionary<string, string> allParams = ArgsParser.Parse1(_params);
            string value = "";
            if (allParams.TryGetValue("TrangThai1", out value))
                Ma_Tt1 = int.Parse(value);
            else
                Ma_Tt1 = -1;

            if (allParams.TryGetValue("TrangThai2", out value))
                Ma_Tt2 = int.Parse(value);
            else
                Ma_Tt2 = -1;

            string title = "";
            if (sender is ButtonItem)
                title = (sender as ButtonItem).Text;
            if (sender is Node)
                title = (sender as Node).Text;
            AppContext.MainForm.ShowControlInTab(this, title);
        }

        #endregion

        private void Ucl_DsHoSoPhaiTra_Load(object sender, EventArgs e)
        {
            try
            {

                blHoSo = new BL_KPHoSo();
               // _BLDangKyKD.InitDependencyBussines();
                MainGrid.PrimaryGrid.DataSource = blHoSo.FindDangKyKdByKeyword(txtGiaTriCanTim.Text, Ma_Tt1, Ma_Tt2,maVtri);

            }
            catch (Exception ex)
            {
                LogUtil.Logger.Fatal("Lấy dữ liệu", ex);
                KPMessageBox.Show("Lấy dữ liệu:" + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);

            }
        }

        private void bbiTraHoSo_Click(object sender, EventArgs e)
        {
            try
            {
                DataRow currentRow = MainGrid.GetCurrentRow();
                if (currentRow != null)
                {
                    DialogResult confirm = KPMessageBox.Show("Bạn muốn trả hồ sơ có mã " + currentRow["So_Bn"].ToString() + " ?", "Xác nhận", KPMessageBoxButton.YesNo, KPMessageBoxIcon.Question);
                    if (confirm == DialogResult.Yes)
                    {
                        blHoSo.TraHoSo(currentRow["Stt_Rec"].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.Logger.Fatal("Lỗi khi trả hồ sơ", ex);
                KPMessageBox.Show("Lỗi trả hồ sơ:" + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);

            }
        }

        private void bbiExit_Click(object sender, EventArgs e)
        {
            Close();
            Control temp = Parent;
            int parentType = 0;
            while (true)
            {
                if (temp is SuperTabControlPanel)
                {
                    parentType = 1;

                }
                //if (temp is SuperTabItem)
                //{
                //    parentType = 2;
                //}
                if (temp is OfficeForm)
                {
                    parentType = 4;
                }
                if (temp is Form)
                {
                    parentType = 3;
                }

                if (parentType != 0)
                    break;
                else
                    if (temp == null)
                        break;
                    else
                        temp = temp.Parent;
            }

            switch (parentType)
            {
                case 1:
                    SuperTabControlPanel supertabpanel = temp as SuperTabControlPanel;
                    supertabpanel.TabItem.Close();
                    break;
                case 2:
                    //  SuperTabItem tab = temp as SuperTabItem;
                    //tab.Close();
                    break;
                case 3:
                    Form frm = temp as Form;
                    if (frm.Modal)
                        frm.DialogResult = DialogResult.Cancel;
                    else
                        frm.Close();
                    break;
                case 4:
                    OfficeForm barForm = temp as OfficeForm;
                    if (barForm.Modal)
                    {
                        barForm.DialogResult = DialogResult.Cancel;
                    }
                    else
                        barForm.Close();
                    break;
                default:
                    Parent.Controls.Remove(this);
                    this.Dispose();
                    break;
            }
        }

       

        private void MainGrid_GridPreviewKeyDown(object sender, DevComponents.DotNetBar.SuperGrid.GridPreviewKeyDownEventArgs e)
        {
            if (MainGrid.CanHandleGridPreviewKey())
            {


                Message m = new Message();
                e.Handled = DispatchCmdKey(ref m, e.KeyData);

            }
        }

        private void MainGrid_PreRenderRow(object sender, DevComponents.DotNetBar.SuperGrid.GridPreRenderRowEventArgs e)
        {
            DataRow row = MainGrid.GetDataRow(e.GridRow.RowIndex);
            if (row != null)
            {
                if (row["Ma_Tt"].ToString() == Ma_Tt1.ToString())
                {
                    e.GridRow.CellStyles.Default.Background.Color1 = Properties.Settings.Default.NormalColor;
                    e.GridRow.CellStyles.Selected.Background.Color1 = Properties.Settings.Default.NormalColor;
                    e.GridRow.CellStyles.Selected.Background.Color2 = Color.GreenYellow;

                }
                if (row["Ma_Tt"].ToString() == Ma_Tt2.ToString())
                {
                    Color c=Properties.Settings.Default.CancelColor;
                    e.GridRow.CellStyles.Default.Background.Color1 = c;
                    e.GridRow.CellStyles.Selected.Background.Color1 = c;

                    e.GridRow.CellStyles.Selected.Background.Color2 = Color.Red;
                }



            }
        }
    }
}
